﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.Configuration;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;



namespace DotNetNuke.Tests.UI.WatiN.AdministratorTests
{
    [TestFixture]


    public class AdminContentLocalizationTests : ATBase
    {

        #region Test Setup and TearDown

        public bool LanguagePackInstalled = false;

        [SetUp]
        public void SetUp()
        {

            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            if (LanguagePackInstalled)
            {
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                LoginPage loginPage = new LoginPage(homePage);
                if (!homePage.RegisterLink.Text.Contains(TestUsers.Host.DisplayName))
                {
                    if (!homePage.RegisterLink.Text.Equals("Register"))
                    {
                        loginPage.LogoffUser();
                    }
                    loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                }
                //Uninstall the language pack and delete the files
                ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
                ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
                //extensionsPage.ExtensionPackageSelectList.Select("Core Language Pack");
                //System.Threading.Thread.Sleep(1500);
                //extensionsPage.CoreLanguagePackSectionLink.Click();
                System.Threading.Thread.Sleep(2000);
                extensionsPage.GetExtensionUninstallButton(LocaleName, extensionsPage.CoreLanguagePacksTable).Click();
                System.Threading.Thread.Sleep(2000);
                extensionsPage.DeleteExtensionFilesCheckbox.Checked = true;
                extensionsPage.UninstallPackageLink.ClickNoWait();
                extensionsPage.YesConfirmationButton.Click();
                //ConfirmDialogHandler dialog = new ConfirmDialogHandler();
                //using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
                //{
                //    extensionsPage.UninstallPackageLink.ClickNoWait();
                //    dialog.WaitUntilExists();
                //    dialog.OKButton.Click();
                //}
                ieInstance.WaitForComplete();
                LanguagePackInstalled = false;
            }

            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        public void Verify_New_UI_in_Language_Editor()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            
            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);

            //Assert 
            Assert.IsTrue(homePage.ModuleMessageSpan.InnerHtml.Contains("As a site administrator you can manage existing languages.  If you would like more languages please contact the Host User for your site."), homePage.ModuleMessageSpan.InnerHtml);
            Assert.IsTrue(languagePage.SystemDefaultLanguageSpan.Exists);
            Assert.IsTrue(languagePage.DefaultLanguageDropDownLink.Exists);
            Assert.IsFalse(languagePage.EnableLocalizedContentLink.Exists);
            Assert.IsTrue(languagePage.LanguageTable.Exists);

            Assert.IsFalse(languagePage.AddNewLanguageLink.Exists);
            Assert.IsFalse(languagePage.TimeZoneEditorLink.Exists);
            Assert.IsFalse(languagePage.InstallLanguagePackLink.Exists);
            Assert.IsFalse(languagePage.CreateLanguagePackLink.Exists);
            Assert.IsFalse(languagePage.VerifyLanguageResourceFilesLink.Exists);

            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            System.Threading.Thread.Sleep(1500);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");

            //Assert 
            System.Threading.Thread.Sleep(1500);
            Assert.IsTrue(languagePage.SystemDefaultLanguageSpan.Exists);
            Assert.IsTrue(languagePage.DefaultLanguageDropDownLink.Exists);
            Assert.IsFalse(languagePage.EnableLocalizedContentLink.Exists);
            Assert.IsTrue(languagePage.LanguageTable.Exists);

            Assert.IsTrue(languagePage.AddNewLanguageLink.Exists);
            //Assert.IsTrue(languagePage.TimeZoneEditorLink.Exists);
            Assert.IsTrue(languagePage.InstallLanguagePackLink.Exists);
            Assert.IsTrue(languagePage.CreateLanguagePackLink.Exists);
            Assert.IsTrue(languagePage.VerifyLanguageResourceFilesLink.Exists);

            ieInstance.GoTo(SiteURL + "/Host/Host%20Settings.aspx");
            HostSettingsPage hostSettingsPage = new HostSettingsPage(homePage);
            hostSettingsPage.HostEmailTextField.Value = TestUsers.Host.Email;
            hostSettingsPage.AdvancedSettingsTab.Click();
            hostSettingsPage.OtherSettingsTab.Click();
            //hostSettingsPage.OtherSettingsExpandButton.ClickNoWait();
            hostSettingsPage.EnableContentLocalizationCheckbox.Checked = true;
            hostSettingsPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);

            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            System.Threading.Thread.Sleep(1500);

            //Assert 
            Assert.IsTrue(languagePage.EnableLocalizedContentLink.Exists);
        }

        [Test]
        public void Verify_Add_New_Language_Functionality()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            System.Threading.Thread.Sleep(1500);
            LanguagePage languagePage = new LanguagePage(homePage);
            languagePage.AddNewLanguage(LocaleName, "English (United States)");
            System.Threading.Thread.Sleep(1500);
            Span flagSpan = languagePage.GetLanguageFlagSpanByLocaleName(LocaleName);

            //Assert
            Assert.IsTrue(flagSpan.Exists);
            Assert.IsTrue(ieInstance.Image(Find.ByAlt(LanguageCode)).Exists);
            Assert.IsFalse(flagSpan.ClassName.Equals("Language Selected"));

        }

        [Test]
        public void Verify_Enable_Localized_Content_Functionality()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            ieInstance.GoTo(SiteURL + "/Host/Host%20Settings.aspx");
            HostSettingsPage hostSettingsPage = new HostSettingsPage(homePage);
            hostSettingsPage.HostEmailTextField.Value = TestUsers.Host.Email;
            hostSettingsPage.AdvancedSettingsTab.Click();
            hostSettingsPage.OtherSettingsTab.Click();
            hostSettingsPage.EnableContentLocalizationCheckbox.Checked = true;
            hostSettingsPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);

            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);

            extensionsPage.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            System.Threading.Thread.Sleep(1500);
            languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Enabled").Checked = true;
            System.Threading.Thread.Sleep(1500);

            //Act 
            languagePage.EnableLocalizedContentLink.ClickNoWait();
            System.Threading.Thread.Sleep(500);
            languagePage.EnableLocalizedContentConfirmation.ClickNoWait();

            //Assert
            System.Threading.Thread.Sleep(5000);
            Assert.IsFalse(languagePage.EnableLocalizedContentLink.Exists);
            Assert.IsTrue(languagePage.LanguageTable.InnerHtml.Contains("Content Localization"));
            Assert.IsTrue(languagePage.LanguageTable.InnerHtml.Contains("Active"));
            Assert.IsTrue(languagePage.LanguageTable.InnerHtml.Contains("Publish"));
            Assert.IsTrue(languagePage.NoOfPagesImage.Exists);
            Assert.IsTrue(languagePage.TranslatedPagesImage.Exists);
            Assert.IsFalse(languagePage.DefaultLanguageDropDownLink.Exists);

        }

        [Test]
        public void Verify_New_Roles_Will_Appear_In_Language_Editor()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            ieInstance.GoTo(SiteURL + "/Host/Host%20Settings.aspx");
            HostSettingsPage hostSettingsPage = new HostSettingsPage(homePage);
            hostSettingsPage.HostEmailTextField.Value = TestUsers.Host.Email;
            hostSettingsPage.AdvancedSettingsTab.Click();
            hostSettingsPage.OtherSettingsTab.Click();
            hostSettingsPage.EnableContentLocalizationCheckbox.Checked = true;
            hostSettingsPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);

            //Add a languauge
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);
            languagePage.AddNewLanguage(LocaleName, "English (United States)");
            System.Threading.Thread.Sleep(1500);
            languagePage.EnableLocalizedContentLink.ClickNoWait();
            System.Threading.Thread.Sleep(2500);
            languagePage.EnableLocalizedContentConfirmation.ClickNoWait();
            System.Threading.Thread.Sleep(3000);
            
            //Act 
            languagePage.GetEditLanguageButtonForLanguage(EnglishLocaleName).ClickNoWait();
            
            //Assert 
            Assert.IsTrue(languagePage.LanguageTranslatorsGrid.InnerHtml.Contains("Translator (" + LanguageCode + ")"));
        }

        [Test]
        public void Verify_No_Localization_Area_on_Page_if_No_Language_is_Enabled()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Act
            //Add a languauge
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);
            languagePage.AddNewLanguage(LocaleName, "English (United States)");
            System.Threading.Thread.Sleep(1500);
            languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Enabled").Checked = false;

            System.Threading.Thread.Sleep(1500);
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;
            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.RibbonBarWrapper.SetAttributeValue("class", "mouse Pinned");
                ieInstance.Refresh();
                ribbonBar.EditCurrentPageLink.Click();
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.PageSettingsLink.Click();
            }
            PageSettingsPage pageSettings = new PageSettingsPage(homePage);

            //Assert 
            //Assert.IsTrue(homePage.ModuleTitleSpan.InnerHtml.Contains("Page Settings"));
            Assert.IsFalse(pageSettings.LocalizationTabLink.Exists);
            Assert.IsFalse(homePage.ContentPane.InnerHtml.Contains("Page Culture:"));
            Assert.IsFalse(homePage.ContentPane.InnerHtml.Contains("Ready for Translation"));
            Assert.IsFalse(homePage.ContentPane.InnerHtml.Contains("Page Localization"));
            Assert.IsFalse(homePage.ContentPane.InnerHtml.Contains("Module Localization"));
        }

        [Test]
        public void Verify_Localization_Area_on_Page_if_Content_Localization_Enabled()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Act
            ieInstance.GoTo(SiteURL + "/Host/Host%20Settings.aspx");
            HostSettingsPage hostSettingsPage = new HostSettingsPage(homePage);
            hostSettingsPage.HostEmailTextField.Value = TestUsers.Host.Email;
            hostSettingsPage.AdvancedSettingsTab.Click();
            hostSettingsPage.OtherSettingsTab.Click();
            hostSettingsPage.EnableContentLocalizationCheckbox.Checked = true;
            hostSettingsPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);

            //Add a languauge
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);
            languagePage.AddNewLanguage(LocaleName, "English (United States)");
            System.Threading.Thread.Sleep(1500);
            
            //Enable content localization 
            languagePage.EnableLocalizedContentLink.Click();
            languagePage.EnableLocalizedContentConfirmation.Click();

            System.Threading.Thread.Sleep(2500);
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            System.Threading.Thread.Sleep(2500);
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;
            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.EditCurrentPageLink.Click();
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.PageSettingsLink.Click();
            }

            //Assert 
            //Assert.IsTrue(homePage.ModuleTitleSpan.InnerHtml.Contains("Page Settings"));
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Localization"));
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("English (United States)"));
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains(EnglishLocaleName));
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Page Culture:"));
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Ready for Translation"));
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Page Localization"));
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Module Localization"));
        }


        #endregion
    }
}